package com.example.demo.service.serviceImpl;

import cn.hutool.db.sql.Query;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.conditions.update.UpdateWrapper;
import com.example.demo.entity.*;
import com.example.demo.mapper.EmployeeMapper;
import com.example.demo.mapper.EmployeeRoleMapper;
import com.example.demo.mapper.RoleMapper;
import com.example.demo.mapper.RolePermissionMapper;
import com.example.demo.service.RoleService;
import io.swagger.models.auth.In;
import org.springframework.security.crypto.bcrypt.BCryptPasswordEncoder;
import org.springframework.security.crypto.password.PasswordEncoder;
import org.springframework.stereotype.Service;
import org.springframework.web.bind.annotation.RequestBody;

import javax.annotation.Resource;
import java.util.ArrayList;
import java.util.List;

@Service
public class RoleServiceImpl implements RoleService {

    @Resource
    RoleMapper roleMapper;
    @Resource
    EmployeeRoleMapper employeeRoleMapper;
    @Resource
    RolePermissionMapper rolePermissionMapper;
    @Resource
    EmployeeMapper employeeMapper;

    @Override
    public Integer addRole(Role role){
        QueryWrapper<Role> queryWrapper=new QueryWrapper<>();
        queryWrapper.eq("role_id",role.getRoleId());
        List<Role> res = roleMapper.selectList(queryWrapper);
        if(res.isEmpty()){
            return roleMapper.insert(role);
        }else{
            return 0;
        }
    }
    public Integer delRole(Integer roleId){
        return roleMapper.deleteById(roleId);
    }

    public Integer addEmployeeRole(Integer employeeId, Integer roleId){
        EmployeeRole employeeRole = new EmployeeRole(employeeId,roleId);
        return employeeRoleMapper.insert(employeeRole);
    }

    public Integer delEmployeeRole(Integer employeeId){
        UpdateWrapper<EmployeeRole> updateWrapper=new UpdateWrapper<>();
        updateWrapper.eq("employee_id",employeeId);
        return employeeRoleMapper.delete(updateWrapper);
    }

    public Integer addRolePermission(Role role, Integer permissionId){
        QueryWrapper queryWrapper = new QueryWrapper();
        queryWrapper.eq("role_id",role.getRoleId());
        queryWrapper.eq("permission_id",permissionId);
        List<RolePermission> res = rolePermissionMapper.selectList(queryWrapper);
        if(res != null){
            return 0;
        }
        RolePermission rolePermission = new RolePermission(role.getRoleId(),permissionId);
        return rolePermissionMapper.insert(rolePermission);
    }

    public Integer delRolePermission(RolePermission rolePermission) {
        return rolePermissionMapper.deleteById(rolePermission.getPermissionId());
    };


    @Override
    public List<Role> selectAllRoles(){
        List<Role> roles=roleMapper.selectList(null);
        return roles;
    }

    @Override
    public Integer saveOrUpdate(Integer roleId,Integer[] permissionId){
        try {
            QueryWrapper<RolePermission> queryWrapper=new QueryWrapper<>();
            queryWrapper.eq("role_id",roleId);
            rolePermissionMapper.delete(queryWrapper);
            for(Integer id:permissionId){
                RolePermission rolePermission=new RolePermission(roleId,id);
                rolePermissionMapper.insert(rolePermission);
            }
            return 1;
        }catch (Exception e){
            return 0;
        }
    }

    @Override
    public List<EmployeeVo> selectEmployeesByBusinessId(Integer businessId) {
        QueryWrapper<Employee> queryWrapper=new QueryWrapper();
        queryWrapper.eq("business_id",businessId);
        List<Employee> list= employeeMapper.selectList(queryWrapper);
        List<EmployeeVo> res=new ArrayList<>();
        for(Employee employee:list){
            EmployeeVo employeeVo=new EmployeeVo();
            employeeVo.setEmployeeId(employee.getEmployeeId());
            employeeVo.setBusinessId(employee.getBusinessId());
            employeeVo.setEmployeeName(employee.getEmployeeName());
            employeeVo.setEmployeePassword(employee.getEmployeePassword());
            employeeVo.setEmployeePhone(employee.getEmployeePhone());
            employeeVo.setEmployeeGender(employee.getEmployeeGender());
            employeeVo.setEmployeeStatus(employee.getEmployeeStatus());
            List<String> roles=employeeMapper.selectRoleLabel(employee.getEmployeeId());
            employeeVo.setRoles(roles);
            res.add(employeeVo);
        }
        return res;
    }

    @Override
    public EmployeeVo selectEmployeeByEmployeeId(Integer employeeId) {
        QueryWrapper<Employee> queryWrapper=new QueryWrapper<>();
        queryWrapper.eq("employee_id",employeeId);
        Employee employee=employeeMapper.selectOne(queryWrapper);
        EmployeeVo employeeVo=new EmployeeVo();
        employeeVo.setEmployeeId(employee.getEmployeeId());
        employeeVo.setBusinessId(employee.getBusinessId());
        employeeVo.setEmployeeName(employee.getEmployeeName());
        employeeVo.setEmployeePassword(employee.getEmployeePassword());
        List<String> roles=employeeMapper.selectRoleLabel(employee.getEmployeeId());
        employeeVo.setRoles(roles);
        return employeeVo;
    }

    @Override
    public void updateEmployee(Employee employee) {
        employeeMapper.updateById(employee);
    }

    @Override
    public Integer addEmployee(@RequestBody Employee employee) {
        try {
            PasswordEncoder passwordEncoder= new BCryptPasswordEncoder();
            employee.setEmployeePassword(passwordEncoder.encode("123456"));
            employeeMapper.insert(employee);
            QueryWrapper<Role> queryWrapper=new QueryWrapper<>();
            queryWrapper.eq("role_label",employee.getRole());
            Role role=roleMapper.selectOne(queryWrapper);
            EmployeeRole employeeRole=new EmployeeRole(employee.getEmployeeId(),role.getRoleId());
            employeeRoleMapper.insert(employeeRole);
            return 1;
        }catch (Exception e){
            return 0;
        }

    }

    @Override
    public List<Permission> iteratePermission(List<Permission> permissionList, Integer pid) {
        List<Permission> result=new ArrayList<>();
        for(Permission permission:permissionList){
            Integer permissionId=permission.getPermissionId();
            Integer parentId=permission.getPid();
            if(parentId==pid){
                List<Permission> iteratePermission=iteratePermission(permissionList,permissionId);
                permission.setChildren(iteratePermission);
                result.add(permission);
            }
        }
        return result;
    }

    @Override
    public int updateRole(Role role) {
        UpdateWrapper<Role> updateWrapper=new UpdateWrapper<>();
        updateWrapper.eq("role_id",role.getRoleId());
        return roleMapper.update(role,updateWrapper);
    }
}
